<template>
  <el-dialog
    v-model="dialogFormVisible"
    :title="title"
    width="500px"
    @close="close"
  >
    <el-form ref="formRef" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="流程环节" prop="FlowName">
        <el-input v-model="form.FlowName" />
      </el-form-item>
      <el-form-item label="对应流程状态" prop="OrderFlowState">
        <el-select v-model="form.OrderFlowState">
          <el-option
            v-for="(item, index) in OrderFlowStates"
            :key="index"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { doEdit } from '@/api/systemsetBusinessFlow'

  export default defineComponent({
    name: 'SystemsetBusinessFlowEdit',
    emits: ['fetch-data'],
    setup(props, { emit }) {
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        OrderFlowStates: [
          { lable: '已签约，待到单录入', value: 1 },
          { lable: '已到单录入，待确认收款', value: 2 },
          { lable: '收款确认', value: 3 },
          { lable: '业务提单', value: 4 },
          { lable: '销售内勤审核/分单', value: 5 },
          { lable: '运营内勤分单', value: 6 },
          { lable: '店长派单', value: 7 },
          { lable: '服务中', value: 10 },
        ],
        formRef: null,
        form: {
          roles: [],
        },
        rules: {
          FlowName: [
            { required: true, trigger: 'blur', message: '请输入标题' },
          ],
          OrderFlowState: [
            { required: true, trigger: 'blur', message: '请选择状态' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      })

      const showEdit = (row) => {
        if (!row) {
          state.title = '添加'
        } else {
          state.title = '编辑'
          state.form = Object.assign({}, row)
        }
        state.dialogFormVisible = true
      }
      const close = () => {
        state['formRef'].resetFields()
        state.form = {
          roles: [],
        }
        state.dialogFormVisible = false
      }
      const save = () => {
        state['formRef'].validate(async (valid) => {
          if (valid) {
            const { msg } = await doEdit(state.form)
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            emit('fetch-data')
            close()
          }
        })
      }

      return {
        ...toRefs(state),
        showEdit,
        close,
        save,
      }
    },
  })
</script>
